import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * <p>Title: Ntsky OpenSource BBS</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 天网文化发展有限公司</p>
 * @author 姚君林
 * @version 1.0
 */

public class XmlOperate{
    private final static Logger logger = Logger.getLogger(XmlOperate.class);
    private static XmlOperate xmlReadWrite = null;
    private static Object obj = new Object();
    private String path = null;
    private XmlOperate(){
    }
    /**
     * 获取读取XML的实例
     * @return XMLReader
     */
    public static XmlOperate getInstance() {
        if (xmlReadWrite == null) {
            synchronized(obj){
                if (xmlReadWrite == null) {
                    xmlReadWrite = new XmlOperate();
                    //logger.info("初始创建xmlReadWrite对象 ：" + xmlReadWrite);
                }
            }
        }
        return xmlReadWrite;
    }
    /**
     * 读取XML文件
     * @param xmlPath String
     * @throws DocumentException
     * @throws Exception
     * @return Document
     */
    public Document read(String xmlPath) throws Exception {
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File(getRealPath(xmlPath)));
        //logger.info("成功载入xml文件");
        return doc;
    }

    /**
     * 写xml
     * @param args String[]
     */
    public void write(Document document,String xmlPath) throws Exception{
        //logger.info("xml的路径 : " +getRealPath(xmlPath));
        XMLWriter writer = new XMLWriter(new FileWriter(getRealPath(xmlPath)),OutputFormat.createPrettyPrint());
        writer.write(document);
        writer.close();
    }

    /**
     * 文件的相对路径
     * @param path String
     * @return String
     */
    public String getRealPath(String path){
        //logger.info("path = " + path);
        URL url = null;
        try{
            url  = this.getClass().getClassLoader().getResource(path); //com.ntsky.properties.
            logger.info("成功获得XML的物理路径 : " + url.getFile());
        }
        catch(Exception e){
            //logger.info("url error :" + e.getMessage());
        }
        return url.getFile();
    }


    public static void main(String[] args) {
        /*System.out.println(XMLReadWrite.getInstance().getRealPath(XMLPath.NTSKY_COPYLEFT));

    //File.getAbsolutePath();*/

    try {
        Document document = XmlOperate.getInstance().read("");
        System.out.println("ok");


        Element root = document.getRootElement();
        System.out.println("root name = " + root.getName());

        List list_linkId = document.selectNodes("//link/@linkId");
        Iterator rs_linkId = list_linkId.iterator();
        System.out.println("rs_linkId = "+ rs_linkId.hasNext());

        while (rs_linkId.hasNext()) {

            //root.remove(e);
            //Node node = document.selectSingleNode("//ntsky/link");
            //System.out.println("node text : " + node.getName());
            //System.out.println("删除" + (document.remove(root)));
            //System.out.println("删除" + (root.remove(node)));
            //document.remove(node);
            Iterator rs_root = root.elementIterator();
            Node node = null;
            String linkId = null;

            for (; rs_root.hasNext(); ) {
                node = (Node) rs_root.next();

                //List list_root = document.selectNodes("//link");
                //Element e = (Element)rs_linkId.next();

                Attribute attribute = (Attribute) rs_linkId.next();
                linkId = attribute.getText();
                System.out.println("linkId = " + linkId);
                //获取节点
                //element = (Element) rs_root.next();
                //System.out.println("root remove : " +root.remove(element));
                //System.out.println("element  : " + element.getName());
                //Iterator rs = element.elementIterator("url");
                System.out.println("linkId isTrue : " + linkId.equals("14"));
                if (linkId.equals("14")) {
                    System.out.println("该element为　：" + node.getName());
                    System.out.println("成功 isTrue : " + root.remove(node));
                    System.out.println(document.remove(node)); ;
                    System.out.println("删除节点");
                    XmlOperate.getInstance().write(document , "");
                }
            }
        }

          System.out.println("执行");
          /*
        System.out.println("root : " + root.getText());
        //枚举所有的节点
        //List list = document.selectNodes("//link");
        //Iterator rs_root = list.iterator();
        //while (rs_root.hasNext()) {
        List list_linkId = document.selectNodes("//link/@linkId");
        Iterator rs_linkId = list_linkId.iterator();
        while (rs_linkId.hasNext()) {
            Iterator rs_root = root.elementIterator();
             for (;rs_root.hasNext();){
            //获取节点
            Element element = (Element) rs_root.next();
            Iterator rs = element.elementIterator("url");

                Attribute attribute = (Attribute) rs_linkId.next();
                String linkId = attribute.getText();
                System.out.println("linkId = " + linkId);

                while (rs.hasNext()) {

                    Element n = (Element) rs.next();

                    System.out.println(n.getName());

                    System.out.println(n.getText());
                }
            }
        }
        */
    }

    catch (DocumentException ex) {
    }
    catch (Exception ex) {
    }
    }
}
